package ru.spbsu.electromenu.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.spbsu.electromenu.model.Alert;
import ru.spbsu.electromenu.model.Session;
import ru.spbsu.electromenu.model.Table;
import ru.spbsu.electromenu.service.AlertService;
import ru.spbsu.electromenu.service.SessionService;
import ru.spbsu.electromenu.service.TableService;
import ru.spbsu.electromenu.util.collections.CollectionUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Date: 5/5/11
 * Time: 1:11 AM
 *
 * @author Artem Titov
 */
public class ShowAlertServlet extends EditObjectServlet {
    private static final Logger logger = LoggerFactory.getLogger(ShowAlertServlet.class);
    private AlertService alertService;
    private SessionService sessionService;
    private TableService tableService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        alertService = getBean("alertService");
        sessionService = getBean("sessionService");
        tableService = getBean("tableService");
        if (alertService == null || sessionService == null || tableService == null) {
            logger.error("Failed to instantiate necessary services");
            isValid = false;
            throw new IllegalStateException("Failed to instantiate necessary services");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int result = HttpServletResponse.SC_BAD_REQUEST;
        if (request.getParameter("delete") != null) {
            final int alertId = getParameter(request, "alertId", -1);
            if (alertId > 0) {
                alertService.delete(alertId);
                result = HttpServletResponse.SC_OK;
            } else {
                result = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
            }
        }
        if (result != HttpServletResponse.SC_OK) {
            response.sendError(result);
        } else {
            response.sendRedirect("showAlert");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (!isValid) {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Can't initialize environment");
            return;
        }
        final Map<Integer, Table> tablesMap = CollectionUtils.mapFromCollection(tableService.getAll());
        final List<Session> sessions = sessionService.getAll();
        for (final Session session : sessions) {
            session.injectTable(tablesMap);
        }
        final Map<Integer, Session> sessionsMap = CollectionUtils.mapFromCollection(sessions);
        final List<Alert> alerts = alertService.getAll();
        for (final Alert alert : alerts) {
            alert.injectSession(sessionsMap);
        }
        request.setAttribute("alerts", alerts);
        final RequestDispatcher dispatcher = request.getRequestDispatcher("showAlert.jsp");
        if (dispatcher != null) {
            dispatcher.forward(request, response);
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}
